/*
 * Copyright (c) 2001, 2002 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.perforce.core.internal.api;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import net.sourceforge.perforce.api.IPerforceCommand;
import net.sourceforge.perforce.core.PerforceCorePlugin;
import net.sourceforge.perforce.core.api.IChangesResult;
import net.sourceforge.perforce.core.api.IPerforceServer;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.PlatformObject;

/**
  * ClientUser object for the 'p4 changes' command.
  * 
  * @version $Revision: 16 $
  * @author  <a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class Changes extends AbstractPerforceCommand implements IPerforceCommand
{
	// --------------------------------------------------------------------------- Private Variables

	/** Stores the result the of the operation in form of a list. */	
	private List result = new ArrayList();
	/** Returns a reference to the responsible perforce server.
	  * TODO: Remove
	  */
	private IPerforceServer perforceServer;
	
	// --------------------------------------------------------------- Public Static Factory Methods
	
	/** Convenience method for creating instances of Changes.
	  * 
	  * @param perforceServer the responsible server.
	  * @return the Changes instance.
	  */
	public static final Changes newInstance(IPerforceServer perforceServer)
	{
		MultiStatus status = new MultiStatus(PerforceCorePlugin.ID, IStatus.OK, "", null); 
		return new Changes(status, perforceServer);
	}

	// ------------------------------------------------------------------------ Private Constructors
	
	/** Constructor for Changes.
	  * 
	  * @param status the MultiStatus object.
	  * @param perforceServer the Perforce Server.
	  */
	private Changes(MultiStatus status, IPerforceServer perforceServer)
	{
		super(status);
		this.perforceServer = perforceServer;
	}

	// -------------------------------------------------------- Public Methods (IClientUserDelegate)
 
	/** @see IClientUserDelegate#outputStat(Map) */
	public void outputStat(Map strDict)
	{
		Result tempResult = new Result();
		tempResult.setChangelist(Helper.safeGetInteger(strDict, "change"));            //$NON-NLS-1$
		tempResult.setDate(Helper.safeGetDate(strDict, "time"));                       //$NON-NLS-1$
		tempResult.setUserName(Helper.safeGetString(strDict, "user"));                 //$NON-NLS-1$
		tempResult.setClientName(Helper.safeGetString(strDict, "client"));             //$NON-NLS-1$
		tempResult.setStatus(Helper.safeGetString(strDict, "status"));                 //$NON-NLS-1$
		tempResult.setDescription(Helper.safeGetString(strDict, "desc"));              //$NON-NLS-1$
		tempResult.setPerforceServer(perforceServer);
			
		result.add(tempResult);
	}

	// ----------------------------------------------------------- Public Methods (IPerforceCommand)
	
	/** @see IPerforceCommand#getCommand() */
	public String getCommand()
	{
		return "changes";                                                              //$NON-NLS-1$
	}


	/** @see IPerforceCommand#getStatus() */
	public IStatus getStatus()
	{
		return getMultiStatus();
	}

	/** @see IPerforceCommand#isHidingFromConsole() */
	public boolean isHidingFromConsole()
	{
		return false;
	}

	// ------------------------------------------------------------------------------ Public Methods

	/** Returns the result of the operation.
	  * 
	  * @return array of result objects.
	  */
	public IChangesResult[] getResult()
	{
		return (IChangesResult[]) result.toArray(new IChangesResult[result.size()]);
	}
	
	// ---------------------------------------------------------------------- Private Nested Classes

	/**
	  * Implementation of the IChangesResult. Stores information provided
	  * by the 'p4 changes' command.
	  * 
	  * @see IChangesResult
	  */
	private static class Result extends PlatformObject implements IChangesResult
	{
		// ----------------------------------------------------------------------- Private Variables

		/** Stores the changelist number. */
		private Integer changelist;
		/** Stores the client name. */
		private String clientName;
		/** Stores the submission date. */
		private Date date;
		/** Stores the description. */
		private String description;
		/** Stores the status. */
		private String status;
		/** Stores the user name. */
		private String userName;
		/** Stores the perforce server.
		  * TODO: Remove
		  */
		private IPerforceServer perforceServer;
		
		// -------------------------------------------------------------------------- Public Methods
		
		/** @see IChangesResult#getChangelist() */
		public Integer getChangelist() 
		{ 
			return changelist; 
		}

		/** @see IChangesResult#getClientName() */
		public String getClientName() 
		{ 
			return clientName; 
		}

		/** @see IChangesResult#getDate() */
		public Date getDate() 
		{ 
			return date; 
		}

		/** @see IChangesResult#getDescription() */
		public String getDescription() 
		{ 
			return description; 
		}

		/** @see IChangesResult#getStatus() */
		public String getStatus() 
		{ 
			return status; 
		}

		/** @see IChangesResult#getUserName() */
		public String getUserName() 
		{ 
			return userName; 
		}

		/** @see IChangesResult#getPerforceServer() */
		public IPerforceServer getPerforceServer() 
		{ 
			return perforceServer; 
		}
		
		// --------------------------------------------------------------------------------- Methods

		/** Sets the changelist.
		  * 
		  * @param changelist The changelist to set
		  */
		void setChangelist(Integer changelist) 
		{
			this.changelist = changelist;
		}

		/** Sets the clientName.
		  * 
		  * @param clientName The clientName to set
		  */
		void setClientName(String clientName) 
		{
			this.clientName = clientName;
		}

		/** Sets the date.
		  * 
		  * @param date The date to set
		  */
		void setDate(Date date) 
		{
			this.date = date;
		}

		/** Sets the description.
		  * 
		  * @param description The description to set
		  */
		void setDescription(String description) 
		{
			this.description = description;
		}

		/** Sets the status.
		  * 
		  * @param status The status to set
		  */
		void setStatus(String status) 
		{
			this.status = status;
		}

		/** Sets the userName.
		  * 
		  * @param userName The userName to set
		  */
		void setUserName(String userName) 
		{
			this.userName = userName;
		}

		/** Sets the perforceServer.
		  * 
		  * @param perforceServer The perforceServer to set
		  */
		void setPerforceServer(IPerforceServer perforceServer) 
		{
			this.perforceServer = perforceServer;
		}
	}
}
